package tcp;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * 长连接
 * 加while
 **/
public class 长连接之用户输入Client {
    public static void main(String[] args) throws IOException {
        Log.println("准备建立连接");
            Socket socket = new Socket("127.0.0.1", 9090);//拨号
        Log.println("连接已经建立");
            Scanner systemInScanner=new Scanner(System.in);
        while (systemInScanner.hasNextLine()){//用户想输入就可以一直输入
            String w=systemInScanner.nextLine();
            InputStream is = socket.getInputStream();
            Scanner SocketScanner = new Scanner(is, "UTF-8");
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter writer = new OutputStreamWriter(os, "UTF-8");
            PrintWriter printWriter = new PrintWriter(writer);
            //客户端先发送请求
            printWriter.printf("我是Java19班的\r\n%s\r\n",w);
            printWriter.flush();
            //读取响应
            String header = SocketScanner.nextLine();//读到的是 好的
            String word = SocketScanner.nextLine();//苹果
            System.out.println(word);
        }
//        socket.close();//挂断电话
    }
}
